<?php

namespace V1\Model;


use Think\Exception;
use Think\Model;

class FavouriteModel extends Model
{
    protected $tableName = 'member_favourite';

    /**
     * 根据传入类型获取该类型所对应的值
     * @param $type
     * @return int|null
     */
    private function get_favourite_type($type)
    {
        switch ($type) {
            case 'ask':
                return 1;
                break;
            case 'article':
                return 2;
                break;
            default:
                return null;
                break;
        }
    }

    /**
     * 获取收藏
     * @param $info
     * @return int
     */
    public function get_favourite_info($info)
    {
        $map['uid'] = $info['uid'];
        $map['obj_id'] = $info['ask_id'];
        $result = $this->where($map)->count('uid');
        if($result===false){
            new Exception();
        }else{
            return $result;
        }
    }

    /**
     * 添加收藏
     * @param $info
     * @return bool|int
     */
    public function add_favourite($info)
    {
        //判断是否收藏过
        if (count($this->get_favourite($info['uid'], $info['type'], $info['ask_id'])) > 0) {
            return 217;
        } else {
            $data['uid'] = $info['uid'];
            $data['to_uid'] = $info['to_uid'];
            $data['type'] = $this->get_favourite_type($info['type']);
            $data['cat_id'] = $info['cat_id'];
            $data['obj_id'] = $info['ask_id'];
            $data['addtime'] = time();
            $result = $this->add($data);
            return $result;
        }
    }

    /**
     * 取消收藏
     * @param $info
     * @return bool
     */
    public function remove_favourite($info)
    {
        $map['uid'] = $info['uid'];
        $map['obj_id'] = $info['ask_id'];
        $result = $this->where($map)->delete();
        if ($result > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取当前用户分组收藏
     * @param $uid
     * @param null $type
     * @param null $obj_id
     * @return mixed|null
     */
    private function get_favourite($uid, $type = null, $obj_id = null)
    {
        $question_ask_model = new QuestionAskModel();
        $map['fav.uid'] = $uid;
        if ($type != null) {
            $map['fav.type'] = $this->get_favourite_type($type);
        }
        if ($obj_id != null) {
            $map['fav.obj_id'] = $obj_id;
        }
        $request = $this->alias('fav')
            ->join('LEFT JOIN __MEMBER_PROFILE__ profile ON fav.uid = profile.userid')
            ->field('fav.*,profile.truename,profile.nickname,profile.avatar')
            ->where($map)
            ->select();
        switch ($type) {
            case 'ask':
                $model = $question_ask_model;
                break;
            case 'article':
                break;
            default:
                return null;
                break;
        }
        foreach ($request as $k => $v) {
            $result_map['id'] = $v['obj_id'];
            $result = $model->where($result_map)->select();
            $request[$k]['obj_id'] = $result[0]['id'];
            $request[$k]['obj_content'] = $result[0]['content'];
            $request[$k]['addtime'] = date("Y-m-d H:i", $v['addtime']);
        }
        return $request;
    }


    /**
     * 获取用户收藏列表
     * @param $uid
     * @return mixed
     * @throws Exception
     */
    public function get_simple_favourite($uid)
    {
        $map['fav.uid'] = $uid;
        $request = $this->alias('fav')
            ->join('LEFT JOIN __MEMBER_PROFILE__ profile ON profile.userid = fav.to_uid')
            ->join('LEFT JOIN __QUESTION_ASK__ ask ON ask.id = fav.obj_id')
            ->field('fav.to_uid,profile.truename,profile.address,profile.avatar,profile.memberships,ask.id,ask.content,ask.addtime,ask.thumb0,ask.thumb1,ask.thumb2,ask.thumb3,ask.thumb4,ask.thumb5,ask.areaid,ask.catid,ask.answer_number,ask.gold')
            ->where($map)
            ->order('fav.addtime desc')
            ->select();
        if($request===false){
            throw new Exception(C('STATUS')['101']);
        }else{
            foreach ($request AS $k => $v) {
                $result = D('MemberProfile')->get_user_details_ask_list($v['to_uid']);
                $request[$k]['grade'] = $result['grade'];
                $request[$k]['answer_count'] = D('QuestionAnswer')->get_answer_number($v['to_uid']);
                $request[$k]['memberships'] = $result['profile'][0]['member_profile']['memberships'];
                $request[$k]['medal'] = $result['medal'];
            }
        }
        return $request;
    }
}